package com.trsvax.gwthello.client.gwtui.widgets;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.trsvax.gwthello.client.gwtui.panel.CategoryPanel;

public class CategoryPanelWidget extends Composite {

	Element element;
	Dictionary dictionary;
	CategoryPanel widget;

	public CategoryPanelWidget(Element element, Dictionary dictionary) {
		this.element = element;
		this.dictionary = dictionary;
		widget = new CategoryPanel(Unit.PX);
		init();
		initWidget(widget);
	}

	void init() {

		NodeList<Element> nl = element.getElementsByTagName("dt");

		for (int i = 0; i < nl.getLength(); i++) {
			HTML w = new HTML();
			String head = nl.getItem(i).getInnerText();
			String sub = "";
			Element dd = nl.getItem(i).getNextSiblingElement();
			while (true) {

				if (dd == null)
					break;
				if (dd.getTagName().equals("DD")) {
					String text = dd.getInnerText();
					sub += text + "<br/>";
				}
				if (dd.getTagName().equals("DT")) {
					break;
				}
				dd = dd.getNextSiblingElement();
			}
			w.setHTML(sub);

			widget.add(w, head);
		}

	}

}
